home *** CD-ROM | disk | FTP | other *** search
/ Cream of the Crop 25 / Cream of the Crop 25.iso / program / gmp202.zip / mpf / out_str.c < prev    next >
C/C++ Source or Header  |  1996-05-17  |  2KB  |  90 lines

  1. /* mpf_out_str (stream, base, n_digits, op) -- Print N_DIGITS digits from
  2.    the float OP to STREAM in base BASE.  Return the number of characters
  3.    written, or 0 if an error occurred.
  4.  
  5. Copyright (C) 1996 Free Software Foundation, Inc.
  6.  
  7. This file is part of the GNU MP Library.
  8.  
  9. The GNU MP Library is free software; you can redistribute it and/or modify
  10. it under the terms of the GNU Library General Public License as published by
  11. the Free Software Foundation; either version 2 of the License, or (at your
  12. option) any later version.
  13.  
  14. The GNU MP Library is distributed in the hope that it will be useful, but
  15. WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
  16. or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU Library General Public
  17. License for more details.
  18.  
  19. You should have received a copy of the GNU Library General Public License
  20. along with the GNU MP Library; see the file COPYING.LIB.  If not, write to
  21. the Free Software Foundation, Inc., 59 Temple Place - Suite 330, Boston,
  22. MA 02111-1307, USA. */
  23.  
  24. #include <stdio.h>
  25. #include "gmp.h"
  26. #include "gmp-impl.h"
  27.  
  28. size_t
  29. #if __STDC__
  30. mpf_out_str (FILE *stream, int base, size_t n_digits, mpf_srcptr op)
  31. #else
  32. mpf_out_str (stream, base, n_digits, op)
  33.      FILE *stream;
  34.      int base;
  35.      size_t n_digits;
  36.      mpf_srcptr op;
  37. #endif
  38. {
  39.   char *str;
  40.   mp_exp_t exp;
  41.   size_t written;
  42.   TMP_DECL (marker);
  43.  
  44.   TMP_MARK (marker);
  45.  
  46.   if (base == 0)
  47.     base = 10;
  48.   if (n_digits == 0)
  49.     n_digits = (((op->_mp_prec - 1) * BITS_PER_MP_LIMB)
  50.         * __mp_bases[base].chars_per_bit_exactly);
  51.  
  52.   if (stream == 0)
  53.     stream = stdout;
  54.  
  55.   str = (char *) TMP_ALLOC (n_digits + 2); /* extra for minus sign and \0 */
  56.  
  57.   mpf_get_str (str, &exp, base, n_digits, op);
  58.   n_digits = strlen (str);
  59.  
  60.   written = 0;
  61.  
  62.   /* Write sign */
  63.   if (str[0] == '-')
  64.     {
  65.       str++;
  66.       fputc ('-', stream);
  67.       written = 1;
  68.     }
  69.  
  70.   fwrite ("0.", 1, 2, stream);
  71.   written += 2;
  72.  
  73.   /* Write mantissa */
  74.   {
  75.     size_t fwret;
  76.     fwret = fwrite (str, 1, n_digits, stream);
  77.     written += fwret;
  78.   }
  79.  
  80.   /* Write exponent */
  81.   {
  82.     int fpret;
  83.     fpret = fprintf (stream, (base <= 10 ? "e%ld" : "@%ld"), exp);
  84.     written += fpret;
  85.   }
  86.  
  87.   TMP_FREE (marker);
  88.   return ferror (stream) ? 0 : written;
  89. }
  90.